<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\EatEveryDay;
use Illuminate\Http\Request;
use App\Models\Dashboard;
use Validator , Config , Session , Cookie;

class EatEveryDayController extends AdminController {


    public function index(Request $request){
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 20);
        $search = [];
        $EatEveryDayModel = new EatEveryDay();
        $data=$EatEveryDayModel->EatEveryDay($search, $page, $pageSize);
        $eatEveryDayPrize = new Dashboard();
        $total=$eatEveryDayPrize->CountDay();
        return response()->json(['code' => 0, 'msg' => '', 'data' => $data , 'total' => $total]);
    }

    //修改页面
    public function getEat($id){
        $this->_response['EatEveryDay'] = EatEveryDay::where('id', $id)->first();
        $data=$this->_response;
        return response()->json(['code' => 0, 'msg' => '', 'data' =>$data]);
    }
    public function getEatUpdate(Request $request){
        $data = $request->all();
        if( $data['status']==='下架'){
            $data['status']=0;
        }else {
            $data['status']=1;
        }
        $saveEdit = [
            'id' => $data['id'],
            'status' => $data['status'],
            'probability' => $data['probability'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
        $EatEveryDayModel = new EatEveryDay();
        if($EatEveryDayModel->saveEdit($saveEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function Delete($id){
        $saveEdit =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $EatEveryDayModel = new EatEveryDay();
        if($EatEveryDayModel->saveEdit($saveEdit,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }
}